import type { Reducer, Effect } from 'umi';
import { fakeAccountLogin } from '@/services/login';
import { queryCurrent } from '@/services/user';
import { setAuthority } from '@/utils/authority';
import { message } from 'antd';

export type StateType = {
  errorCode?: string;
  type?: string;
  currentAuthority?: 'jxs-1'|'jxs-0'|'jxs-2'|'jxs-3' | 'xs' | 'admin' | 'store' | 'financial'|'jxs2';
};

export type LoginModelType = {
  namespace: string;
  state: StateType;
  effects: {
    login: Effect;
    logout: Effect;
  };
  reducers: {
    changeLoginStatus: Reducer<StateType>;
  };
};

const Model: LoginModelType = {
  namespace: 'login',

  state: {
    errorCode: undefined,
  },

  effects: {
    *login({ payload }, { call, put }) {
      const response = yield call(fakeAccountLogin, payload);
      yield put({
        type: 'changeLoginStatus',
        payload: response,
      });
      // Login successfully
      // console.log(response);
      // if (response.status === 'ok') {
      if (response.errorCode === '200') {
        // const urlParams = new URL(window.location.href);
        message.success(' 登录成功！');
        //保存到缓存中
        sessionStorage.setItem('token',response.data.token);
        const  accessData = yield call(queryCurrent);
        console.log(accessData);
        sessionStorage.setItem('userId',accessData.data.userId);
        sessionStorage.setItem('dealerId',accessData.data.dealerId);
        
        if(accessData.data.access.indexOf('jxs')>=0){
          if(accessData.data.level==='一级'){
            sessionStorage.setItem('level','1');
            sessionStorage.setItem('access',accessData.data.access);
            setAuthority(accessData.data.access);
            window.location.href = '/jf/#/jxs';
          }else{

            // console.log(accessData.data.access)
            sessionStorage.setItem('level','2');
            if(accessData.data.access==='jxs2-1'){
              sessionStorage.setItem('access','dealer2');
              setAuthority('dealer2');
              window.location.href = '/jf/#/jxsinfo';
            }else{
              sessionStorage.setItem('access',accessData.data.access);
              setAuthority(accessData.data.access);
              // setAuthority(accessData.data.access.replace(/jxs2/, jxs2));
              window.location.href = '/jf/#/jxsinfo';
            }
          }
        }else{
          sessionStorage.setItem('access',accessData.data.access);
          sessionStorage.setItem('sale_type',accessData.data.SalesType);
          if(accessData.data.SalesType){
            if(accessData.data.SalesType==='1'){
              setAuthority('sale0');
            }else{
              setAuthority(accessData.data.access);
            }
          }else{
            setAuthority(accessData.data.access);
          }
          //TODO 整下
          if(accessData.data.access==='sale'){
           window.location.href = '/jf/#/sale';
          }else{
           window.location.href = '/jf/#/welcome';
        }
        }
      }
    },

    logout() {
      //清空下token和缓存
      sessionStorage.removeItem('access');
      setAuthority('');
      sessionStorage.removeItem('token');
      sessionStorage.removeItem('antd-pro-authority');
      
      // sessionStorage.removeItem('token');
      // const { redirect } = getPageQuery();
      // Note: There may be security issues, please note
      if (window.location.pathname !== '/user/login') {
        // history.replace({
        //   pathname: '/user/login',
         
        // });
        
         window.location.href = '/jf/#/user/login';
         window.location.reload();
      }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        errorCode: payload.errorCode,
        type: payload.type,
      };
    },
  },
};

export default Model;